//202302104036崔晓涵
package cn.edu.sdjzu.xg.eagle.dao.mapper;

import cn.edu.sdjzu.xg.eagle.dao.StudentDao;
import cn.edu.sdjzu.xg.eagle.entity.Student;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.factory.ServiceFactory;
import cn.edu.sdjzu.xg.eagle.service.UserService;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDaoImpl implements StudentDao {

    @Override
    public boolean add(Connection connection, Student student) throws SQLException, BaseException, InvocationTargetException {
        String sql_select = "INSERT INTO bysj22.student (id, no, name, user_id) " +
                "VALUES (?, ?, ?, ?)";
        PreparedStatement add_preparedStatement =
                connection.prepareStatement(sql_select);
        //给参数赋值
        add_preparedStatement.setLong(1, student.getId());
        add_preparedStatement.setString(2, student.getNo());
        add_preparedStatement.setString(3, student.getName());
        User user = student.getUser();
        if(user != null) {
            //用teacher对象的user字段的id为teacher表外键user_id赋值
            add_preparedStatement.setInt(4, student.getUser().getId());
        }
        //执行SQL
        int affectedRowNum = add_preparedStatement.executeUpdate();
        return affectedRowNum > 0;
    }

    @Override
    public boolean delete(Connection connection, Long id) throws SQLException, BaseException, InvocationTargetException {
        //准备预编译的语句
        String sql_delete_id = "DELETE FROM bysj22.student WHERE id = ?";
        PreparedStatement pstmt_delete =
                connection.prepareStatement(sql_delete_id);

        pstmt_delete.setLong(1, id);

        //执行预编译语句，影响的行数保存在rowAffected中
        int rowAffected = pstmt_delete.executeUpdate();

        JdbcHelper.close(pstmt_delete,connection);
        return rowAffected > 0;
    }
    //202302104036崔晓涵
    @Override
    public Student findByid(Connection connection, Long id) throws SQLException, BaseException, InvocationTargetException {
        String sql_select= "SELECT * FROM bysj22.student WHERE id = ?";
        PreparedStatement pstmt_select =
                connection.prepareStatement(sql_select);
        pstmt_select.setLong(1,id);
        ResultSet resultSet_select = pstmt_select.executeQuery();
        Student desiredStudent = null;
        //如果表中存在id对应的记录，则获得表中的字段值，并创建对象
        if(resultSet_select.next()){
            String name = resultSet_select.getString("name");
            String no = resultSet_select.getString("no");
            Long userId = resultSet_select.getLong("user_id");

            User user = ServiceFactory.getService(UserService.class,"UserServiceImpl").find(Math.toIntExact(userId));
            desiredStudent = new Student(id, name, no, userId);
        }
        //关闭三个资源
        JdbcHelper.close(resultSet_select, pstmt_select, connection);
        return desiredStudent;
    }
}
